// pages/store_detail/store_detail.js
const http = require('../../utils/http')
const api = require('../../utils/api')
const util = require('../../utils/util')

import Dialog from "../../components/dialog/dialog"

Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    storeData: null,
    marquee: {
      textLen: 0,
      size: 14,
      icon: '/pages/images/laba.png',
    },
    desCount: 4,
    desContents: [
      "小程序预约下单",
      "到达门店后，点击确认到店，开始订单并开门",
      "进入预定包间，开始体验",
      "时间到，携带好随身物品离店，保洁人员进入打扫"
    ],
    imageUrls: [
      "/pages/images/pre_order.png",
      "/pages/images/sure_order.png",
      "/pages/images/play.png",
      "/pages/images/time_end.png"
    ],
  },

  refreshData() {
    let sid = getApp().globalData.selectStoreId
    http.request({
      showLoading: true,
      url: api.storeDetail,
      data: {
        storeId: sid,
        memberId: getApp().globalData.memberId,
        regionLatitude: getApp().globalData.latitude,
        regionLongitude: getApp().globalData.longitude
      }
    }).then(res=>{
      //处理数据
      let data = res.data.result
      let images = data.store.storeImages.split(',')
      data.store.storeImages = images

      let marquee = this.data.marquee
      var ctx = wx.createCanvasContext('canvas')
      ctx.setFontSize(marquee.size)
      marquee.textLen = ctx.measureText(data.activityDesc).width

      this.setData({
        storeData: data,
        marquee: marquee,
      });

    }).catch(err=>{
      console.log(err)
    });
  },

  navigate() {
    ////使用微信内置地图查看标记点位置，并进行导航
    wx.openLocation({
      latitude: parseFloat(this.data.storeData.store.regionLatitude), //要去的纬度-地址
      longitude: parseFloat(this.data.storeData.store.regionLongitude), //要去的经度-地址
      scale: 16, // 缩放比例
      name: this.data.storeData.store.storeName,
      address: this.data.storeData.store.storeAddressDetail
    })
  },

  connectMerchant() {
    let phone = this.data.storeData.store.tel
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },

  contactCompany(event) {
    let phone = event.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },

  showWifiDialog() {
    let setting = this.data.storeData.storeSettingVO
    let password = setting.password
    let ssid = setting.wifi
    Dialog.confirm({
      selector: "#wifi-dialog",
      title: "店铺WIFI",
      cancelButtonText: "取消",
      confirmButtonText: "一键连接",
      confirmCallback:() => {
        util.wifiConnect({
          ssid: ssid,
          password: password
        })
      },
    });
  },

  toPurchase() {
    let tel = getApp().globalData.tel
    if (tel == "" || tel == null) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
    }
    else {
      wx.navigateTo({
        url: '/pages/purchase/purchase',
      })
    }
  },

  toGroup() {
    wx.navigateTo({
      url: '/pages/group/group'+util.paramsWrap({qrcode: this.data.storeData.storeSettingVO.qrCode}),
    })
  },

  toStoreList() {
    wx.navigateTo({
      url: '/pages/store_list/store_list',
    })
  },

  showStoreDialog(event) {
    let itemId = event.detail
    console.log('itemId', itemId)
    Dialog.confirm({
      selector: "#store-dialog",
      title: "亲爱的顾客朋友",
      cancelButtonText: "换包预订",
      confirmButtonText: "继续预订",
      confirmCallback:() => {
        wx.navigateTo({
          url: '/pages/submit_order/submit_order?itemId='+itemId
        })
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.stopPullDownRefresh()
    getApp().watch('selectStoreId',(v)=>{
			this.refreshData()
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let sid = getApp().globalData.selectStoreId
    if (sid!=""&&sid!=null&&sid!=undefined) {
      this.refreshData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
